<?php
class ServiceOrdersController extends AppController {  
	public $helpers = array('Html', 'Form', 'Session', 'Javascript');
	public $components = array('Session');
    public $uses = array('ServiceOrder','IvaCondition','ServiceOrderRow','Seller','Client');

 public function index() {
        $this->set('service_orders', $this->ServiceOrder->find('all'));
    }
    
 public function pdf($id = null) {
    		if (!$id) {
    			$this->Session->setFlash('Sorry, there was no PDF selected.');
    			$this->redirect(array('action'=>'index'), null, true);
    		}
    		$this->set('service_order', $this->ServiceOrder->read(null, $id));
    		$this->set('service_order_rows', $this->ServiceOrderRow->find('all',array('conditions' => array('service_order_id' => $id))));
    		$this->layout = 'pdf'; //this will use the pdf.ctp layout
    		//$this->set('contenido', $this->generarTablaHTML());
    		$this->render();
 
 }
 public function pdfitema($id = null) {
            if (!$id) {
                $this->Session->setFlash('Sorry, there was no PDF selected.');
                $this->redirect(array('action'=>'index'), null, true);
            }
            $this->set('service_order', $this->ServiceOrder->read(null, $id));
            //$iditem = $order['ServiceOrderRow']['id'];
            //$iditem = $this->ServiceOrderRow->id;
            //$iditem = '47';
            $this->set('service_order_rows', $this->ServiceOrderRow->find('all',array('conditions' => array('service_order_id' => $id, 'ServiceOrderRow.unidad'=> 'A'))));
            $this->layout = 'pdfitem'; //this will use the pdf.ctp layout
            //$this->set('contenido', $this->generarTablaHTML());
            $this->render();
 
 }
               		
public function pdfitemb($id = null) {
            if (!$id) {
                $this->Session->setFlash('Sorry, there was no PDF selected.');
                $this->redirect(array('action'=>'index'), null, true);
            }
            $this->set('service_order', $this->ServiceOrder->read(null, $id));
            //$iditem = $order['ServiceOrderRow']['id'];
            //$iditem = $this->ServiceOrderRow->id;        
            $this->set('service_order_rows', $this->ServiceOrderRow->find('all',array('conditions' => array('service_order_id' => $id, 'ServiceOrderRow.unidad'=> 'B'))));
            $this->layout = 'pdfitem'; //this will use the pdf.ctp layout
            //$this->set('contenido', $this->generarTablaHTML());
            $this->render();
 
 }
//  
    public function view($id) {
    	$this->set('service_order', $this->ServiceOrder->read(null, $id));
    	$this->set('service_order_rows', $this->ServiceOrderRow->find('all',array('conditions' => array('service_order_id' => $id))));
      	
    }
    
    
    public function add() {    	
    	$this->set('iva_conditions', $this->IvaCondition->find('list', array('fields' => array('id', 'nombre'))));
    	$this->set('clients', $this->Client-> find('list', array('fields' => array('id', 'nombre'), 'order' => array('nombre' => 'asc'))));
        $this->set('sellers', $this->Seller->find('list', array('fields' => array('id', 'nombre'))));
        
    	
        if ($this->request->is('post')) {   		
    		$this->ServiceOrder->create();
    		if ($this->ServiceOrder->save($this->request->data)) {
    			$this->Session->setFlash('Se han guardado los cambios.');
    			$this->redirect(array('action' => 'view', $this->ServiceOrder->id ));

                //combo clientes
                //$client = new Client();
                //$clients = $client->find('list', array('fields' => array('Client.id', 'Client.nombre')));
                //$this->set('clients', $clients);       

    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    
    public function edit($id = null) {
    	$this->set('iva_conditions', $this->IvaCondition->find('list', array('fields' => array('id', 'nombre'))));
    	$this->ServiceOrder->id = $id;
    	if ($this->request->is('get')) {
    		$this->request->data = $this->ServiceOrder->read();
    	} else {
    		if ($this->ServiceOrder->save($this->request->data)) {
    			$this->Session->setFlash('Datos actualizados correctamente.');
    			$this->redirect(array('action' => 'index'));
    		} else {
    			$this->Session->setFlash('No se pudieron guardar los cambios.');
    		}
    	}
    }
    
    public function beforeFilter() {
    	parent::beforeFilter();
    	$this->Auth->allow('index', 'view');
    }

    }
    ?>